layui.use(['table', 'admin', 'ax', 'func','element','jquery','upload','util','fileChoose'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var util = layui.util;
    var fileChoose = layui.fileChoose;
    var dataList = [];
    var upload = layui.upload;


    // 渲染列表
    function renderList(dir) {
        dir || (dir = $('#tv-current-position').text());
        layer.load(2);
        $.get('/assets/dms/directoryFile/style/files.json', {
            dir: dir
        }, function (res) {
            layer.closeAll('loading');
            dataList = res.data;
            var htmlStr = fileChoose.renderList({
                data: dataList,
                menu: [{
                    name: '预览',
                    event: 'preview'
                }, {
                    name: '复制',
                    event: 'copy'
                }, {
                    name: '<span style="color: red;">删除</span>',
                    event: 'del'
                }]
            });
            $('#file-list-group').html(htmlStr);
        }, 'json');
    }

    renderList();

    // 列表点击事件
    $(document).off('click.fclip').on('click.fclip', '#file-list-group .file-choose-list-item', function (e) {
        var item = dataList[$(this).data('index')];
        if (item.isDir) {  // 是否是文件夹
            var cDir = $('#tv-current-position').text();
            cDir += (cDir == '/' ? item.name : ('/' + item.name));
            $('#tv-current-position').text(cDir);
            renderList(cDir);
        } else {
            var $cMenu = $(this).find('.file-choose-oper-menu');
            $('.file-choose-oper-menu').not($cMenu).removeClass('show');
            $cMenu.toggleClass('show');
            e.stopPropagation();
        }
    });
    // 点击空白隐藏下拉框
    $(document).off('click.fclomp').on('click.fclomp', function (e) {
        $('.file-choose-oper-menu').removeClass('show');
    });

    // 菜单事件监听
    $(document).off('click.fclomip').on('click.fclomip', '#file-list-group .file-choose-oper-menu-item', function () {
        var event = $(this).data('event');
        var dataIndex = $(this).parent().parent().data('index');
        if (event == 'del') {  // 删除
            layer.confirm('确定要删除此文件吗？', {shade: .1}, function () {
                layer.msg('操作成功', {icon: 1});
                renderList();
            });
        } else if (event == 'preview') {
            window.open(dataList[dataIndex].url);
        } else {
            layer.msg('点击了' + event + '，索引' + dataIndex, {icon: 1});
        }
    });

    // 刷新
    $('#file-btn-refresh').click(function () {
        renderList();
    });

    // 返回上级
    $('#file-btn-back').click(function () {
        var cDir = $('#tv-current-position').text();
        if (cDir != '/') {
            cDir = cDir.substring(0, cDir.lastIndexOf('/'));
            cDir || (cDir = '/');
            $('#tv-current-position').text(cDir);
            renderList(cDir);
        }
    });

    // 上传
    upload.render({
        elem: '#file-btn-upload',
        accept: 'file'
    });

    // 弹窗选择
    $('#file-btn-choose').click(function () {
        fileChoose.open({
            fileUrl: '',
            listUrl: '/assets/dms/directoryFile/style/files.json',
            num: 3,
            dialog: {
                offset: '60px'
            },
            onChoose: function (urls) {
                layer.msg('你选择了：' + JSON.stringify(urls), {icon: 1});
            }
        });
    });




    /**
     * 文件信息管理
     */
    var DirectoryFile = {
        tableId: "directoryFileTable"
    };

    /**
     * 初始化表格的列
     */
    DirectoryFile.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'fileId', hide: true, title: ''},
            {field: 'name', sort: true, title: '文件名称'},
            {field: 'fileType', sort: true, title: '文件类型'},
            {field: 'fileSuffix', sort: true, title: '文件后缀'},
            {field: 'fileSize', sort: true, title: '文件大小'},
            {field: 'url', sort: true, title: '绝对地址'},
            {field: 'path', sort: true, title: '物理地址'},
            {field: 'directoryId', sort: true, title: '目录id'},
            {field: 'owerId', sort: true, title: '拥有者ID'},
            {field: 'isUser', sort: true, title: '是否用户'},
            {field: 'sort', sort: true, title: '排序'},
            {field: 'storageType', sort: true, title: '存储引擎'},
            {field: 'createTime', sort: true, title: '添加时间'},
            {field: 'updateTime', sort: true, title: '修改时间'},
            {field: 'ifDel', sort: true, title: '是否删除'},
            {field: 'deleteTime', sort: true, title: '删除时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    DirectoryFile.search = function () {
        var queryData = {};
        queryData['keyword'] = $("#condition").val();
        table.reload(DirectoryFile.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    DirectoryFile.openAddDlg = function () {
        func.open({
            title: '添加文件',
            content: Feng.ctxPath + '/directoryFile/add',
            tableId: DirectoryFile.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    DirectoryFile.openEditDlg = function (data) {
        func.open({
            title: '修改文件信息',
            content: Feng.ctxPath + '/directoryFile/edit?fileId=' + data.fileId,
            tableId: DirectoryFile.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    DirectoryFile.exportExcel = function () {
        var checkRows = table.checkStatus(DirectoryFile.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    DirectoryFile.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/directoryFile/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(DirectoryFile.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("fileId", data.fileId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + DirectoryFile.tableId,
        url: Feng.ctxPath + '/directoryFile/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: DirectoryFile.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        DirectoryFile.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        DirectoryFile.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        DirectoryFile.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + DirectoryFile.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            DirectoryFile.openEditDlg(data);
        } else if (layEvent === 'delete') {
            DirectoryFile.onDeleteItem(data);
        }
    });
});
